//
//  WSMessageViewController.swift
//  微校园
//
//  Created by Gao on 16/12/12.
//  Copyright © 2016年 weSchool. All rights reserved.
//

import UIKit

class WSMessageViewController: RCConversationListViewController {
    
    override func viewDidLoad() {
        //重写显示相关的接口，必须先调用super，否则会屏蔽SDK默认的处理
        super.viewDidLoad()
        
        setupUI()
        setupRongInfo()
 
    }
    
    //重写RCConversationListViewController的onSelectedTableRow事件
    override func onSelectedTableRow(_ conversationModelType: RCConversationModelType, conversationModel model: RCConversationModel!, at indexPath: IndexPath!) {
        //打开会话界面
        let chat = WSConversationViewController(conversationType: model.conversationType, targetId: model.targetId)
        chat?.title = "想显示的会话标题"
        
        //显示聊天会话界面
        showConversationController(chatController: chat!)
    }
    
}


// MARK: - 设置融云信息
fileprivate extension WSMessageViewController {
    
    func setupRongInfo(){
        //设置需要显示哪些类型的会话
        self.setDisplayConversationTypes([RCConversationType.ConversationType_PRIVATE.rawValue,
                                          RCConversationType.ConversationType_DISCUSSION.rawValue,
                                          RCConversationType.ConversationType_CHATROOM.rawValue,
                                          RCConversationType.ConversationType_GROUP.rawValue,
                                          RCConversationType.ConversationType_APPSERVICE.rawValue,
                                          RCConversationType.ConversationType_SYSTEM.rawValue])
        //设置需要将哪些类型的会话在会话列表中聚合显示
        self.setCollectionConversationType([RCConversationType.ConversationType_DISCUSSION.rawValue,
                                            RCConversationType.ConversationType_GROUP.rawValue])
        
        // 融云空列表时，不显示横线列表
        conversationListTableView.tableFooterView = UIView()
        
        showConnectingStatusOnNavigatorBar = true
        isShowNetworkIndicatorView = false
    }
}

// MARK: - 设置界面
fileprivate extension WSMessageViewController {
    
    func setupUI() {
        
        view.backgroundColor = UIColor.white
        
        // 取消自动缩进 - 如果隐藏了导航栏，会缩进 20 个点
        automaticallyAdjustsScrollViewInsets = false
        
        // 设置右侧导航栏按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add,
                                                            target:self,
                                                            action:#selector(onTapAddItem))
        
        let tableRect = conversationListTableView.frame
        conversationListTableView.frame = CGRect(x: 0, y: WSNavigationBarHeight, width: tableRect.width, height: tableRect.height - WSNavigationBarHeight)
        
    }
    
    /// 发起会话测试
    @objc func onTapAddItem(){
        
        let targetId = "guyanqiu"
        // 启动聊天界面
        //新建一个聊天会话View Controller对象
        let chat = WSConversationViewController()
        //设置会话的类型，如单聊、讨论组、群聊、聊天室、客服、公众服务会话等
        chat.conversationType = RCConversationType.ConversationType_PRIVATE
        //设置会话的目标会话ID。（单聊、客服、公众服务会话为对方的ID，讨论组、群聊、聊天室为会话的ID）
        chat.targetId = targetId
        //设置聊天会话界面要显示的标题
        chat.title = "想显示的会话标题"
        //显示聊天会话界面
        showConversationController(chatController: chat)

        
    }
    
    func showConversationController(chatController :  WSConversationViewController){
        // 隐藏底部的 TabBar
        hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(chatController, animated: true)
        hidesBottomBarWhenPushed = false
    }
}

